/**
 * UI for Warping!
 */
package View;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;

import Model.SolarSystem;

import java.awt.Font;

/**
 * This window allows the player to select a planet to warp to!
 * @author Asa, Whitney
 * @version 1.0
 *
 */
public class WarpWindow extends JPanel{
	
	/**
	 * Dimensions for Universe Panel
	 */
	private final Dimension uiPanelDimensions = new Dimension(200, 225);

	/**
	 * Dimensions for button and text panel
	 */
	private final  Dimension lowPanelDimensions = new Dimension(200, 100);

	/**
	 * Array list of Planet Buttons
	 */
	private final ArrayList<JButton> buttons = new ArrayList<JButton>();

	/**
	 * Images for planets
	 */
	private ImageIcon buttonImg, pressedButtonImg;

	/**
	 * Player Location Icon
	 */
	private final ImageIcon playerHereImg;
	
	/**
	 * Player Location Icon
	 */
	private final ImageIcon pressedPlayerHereImg;
	
	/**
	 * A Temporary Image
	 */
	private final ImageIcon tempImg;
	
	/**
	 * Panel Buttons
	 */
	private final JButton btnWarpHere, btnCancelWarp;

	/**
	 * Panel Labels
	 */
	private final JLabel techLevel, sysName;

	/**
	 * Location Points
	 */
	private Point point;

	/**
	 * Integer Array for Button Press
	 */
	private final int[] buttonPress = new int[3];

	/**
	 * Solar System array
	 */
	private SolarSystem[][] system = new SolarSystem[150][150];

	/**
	 * Constructs the warp view panel
	 * Buttons are created and added to an array of 
	 * buttons where they are stored and accessed
	 * @param s Solar System for universe
	 * @param playerLoc location of the player
	 * @param shipFuel the amount of fuel in the ship
	 * @param fuelCapacity the amount the ship can hold
	 */
	public WarpWindow(SolarSystem[][] s, 
			int[] playerLoc, int shipFuel, int fuelCapacity) {
		system = s;
		buttonPress[0] = -1;
		
		setBackground(Color.GRAY);
		setLayout(new BorderLayout(0, 0));
		
		final JPanel lowerPanel = new JPanel();
		lowerPanel.setBackground(Color.LIGHT_GRAY);
		add(lowerPanel, BorderLayout.SOUTH);
		lowerPanel.setLayout(null);
		lowerPanel.setPreferredSize(lowPanelDimensions);
		
		btnCancelWarp = new JButton("Cancel Warp");
		btnCancelWarp.setBounds(327, 65, 117, 29);
		lowerPanel.add(btnCancelWarp);
		
		final JLabel lblSystemName = new JLabel("System Name:");
		lblSystemName.setFont(new Font("Krungthep", Font.PLAIN, 13));
		lblSystemName.setForeground(Color.BLACK);
		lblSystemName.setBounds(6, 29, 119, 16);
		lowerPanel.add(lblSystemName);
		
		final JLabel lblTechLevel = new JLabel("Tech Level:");
		lblTechLevel.setFont(new Font("Krungthep", Font.PLAIN, 13));
		lblTechLevel.setForeground(Color.BLACK);
		lblTechLevel.setBounds(6, 58, 94, 16);
		lowerPanel.add(lblTechLevel);
		
		final JLabel warningLabel = new JLabel("");
		warningLabel.setForeground(Color.RED);
		warningLabel.setBounds(144, 20, 120, 20);
		warningLabel.setFont(new Font("Krungthep", Font.PLAIN, 10));
		if(((fuelCapacity > 20) && shipFuel < 10) || shipFuel < (fuelCapacity >> 1  )){
			warningLabel.setText("Warning: Fuel Low");
		}
		else{
			warningLabel.setText("");
		}
		lowerPanel.add(warningLabel);
		
		sysName = new JLabel("");
		sysName.setFont(new Font("Krungthep", Font.PLAIN, 11));
		sysName.setForeground(Color.BLACK);
		sysName.setBounds(114, 30, 61, 16);
		lowerPanel.add(sysName);
		
		techLevel = new JLabel("");
		techLevel.setForeground(Color.BLACK);
		techLevel.setFont(new Font("Krungthep", Font.PLAIN, 11));
		techLevel.setBounds(96, 57, 61, 16);
		lowerPanel.add(techLevel);
		
		btnWarpHere = new JButton("WARP HERE");
		btnWarpHere.setBounds(327, 24, 117, 29);
		btnWarpHere.setEnabled(false);
		btnWarpHere.setVisible(false);
		lowerPanel.add(btnWarpHere);
		
		final JPanel uniPanel = new JPanel();
		uniPanel.setBackground(Color.BLACK);
		add(uniPanel, BorderLayout.CENTER);
		uniPanel.setLayout(null);
		uniPanel.setPreferredSize(uiPanelDimensions);
		
		final ButtonListener listen = new ButtonListener();
		btnWarpHere.addActionListener(listen);
		btnCancelWarp.addActionListener(listen);

//Added player location icon
		playerHereImg = new ImageIcon("images/Planet Buttons/playerHere.png");
		pressedPlayerHereImg = new ImageIcon
				("images/Planet Buttons/pressedPlayerHere.png");
		
		tempImg = new ImageIcon("images/Planet Buttons/playerHere.png");
		final JLabel tempLabel = new JLabel("", tempImg, SwingConstants.CENTER);
		tempLabel.setBounds((playerLoc[0] + 10) * 3, (playerLoc[1] + 10) * 3, 10, 10);
		uniPanel.add(tempLabel);

		int counter = 0;
		//parse through universe and create buttons for each system
		for (int i = 0; i < SolarSystem.Width - 1; i++) {
			for (int j = 0; j < SolarSystem.Height - 1; j++) {
				if (s[i][j] != null) {
					String sysName = s[i][j].getSolarName();
					int techlvl = s[i][j].getTechlvl();
					buttonImg = new ImageIcon("images/Planet Buttons/lvl" 
							+ techlvl + ".png");
					pressedButtonImg = new ImageIcon
							("images/Planet Buttons/pressLvl" + techlvl + ".png");
					//add buttons
					if(playerLoc[0] == i && playerLoc[1] == j){
						buttons.add( new JButton("", playerHereImg) );
						buttons.get(counter).setPressedIcon(pressedPlayerHereImg);
						buttons.get(counter).setBounds((i + 10) * 3, 
								(j + 10) * 3, 15, 15);
						buttons.get(counter).setBorder(null);
						tempLabel.setVisible(false);
					}
					else{
						buttons.add( new JButton("", buttonImg) );
						buttons.get(counter).setPressedIcon(pressedButtonImg);
						buttons.get(counter).setBounds
							((i + 10) * 3, (j + 10) * 3, 10, 10);
						buttons.get(counter).setBorder(null);
					}
					
					buttons.get(counter).addActionListener(listen);
					buttons.get(counter).setToolTipText
						("<html>System Name: " + sysName + "<br/>Tech Level: "
							+ techlvl + "</html>");
					buttons.get(counter).setEnabled(s[i][j].getInRange());
					uniPanel.add(buttons.get(counter));
					counter++;
				}
			}
		
		}
	}

	/**
	 * This class is the buttonlistener for warp window. 
	 * Handles all planet buttons as well as the cancel and warp buttons
	 * Cancel warp should return player to playerWindow
	 * @author Whitney
	 */
	private class ButtonListener implements ActionListener{
		/**
		 * Handles all button presses
		 * @param event The button pressed
		 */
		public void actionPerformed (ActionEvent event){
			for(int k = 0; k < buttons.size(); k++){
				if(event.getSource().equals(buttons.get(k))){
					point = buttons.get(k).getLocation();
					int i = (((point.x) / 3) - 10);
					int j = (((point.y) / 3) - 10);
					
					btnWarpHere.setEnabled(true);
					btnWarpHere.setVisible(true);
					
					sysName.setText(system[i][j].getSolarName());
					techLevel.setText("" + system[i][j].getTechlvl());

				}
			}
			if(event.getSource().equals(btnWarpHere)){
				buttonPress[0] = (((point.x) / 3) - 10);
				buttonPress[1] = (((point.y) / 3) - 10);
			}
			if(event.getSource().equals(btnCancelWarp)){
				buttonPress[0] = -2;
			}
			
		}

		/**
		 * The toString for the ButtonListener
		 * @return Returns a space because the button 
		 * listener doesn't need a toString. 
		 * */
		public String toString(){
			return "";
		}
	}

	/**
	 * Returns the button pressed, or, if the warp 
	 * button was pressed, it will return the location 
	 * of the player
	 * Cancel pressed: int[0] = -2
	 * WarpHere pressed: int[0] = location x, int[1] = location y
	 * 
	 * @return the int of the button pressed
	 */
	public int[] getButtonPressed(){
		return buttonPress;
	}
}
